package com.lcn.service.impl;

import com.lcn.mapper.CommentMapper;
import com.lcn.pojo.Comment;
import com.lcn.pojo.Users;
import com.lcn.pojo.bo.CommentBO;
import com.lcn.pojo.vo.CommentVO;
import com.lcn.service.CommentService;
import com.lcn.service.UsersService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UsersService usersService;

    /**
     * 评论朋友圈
     *
     * @param commentBO
     * @return
     */
    @Override
    public CommentVO create(CommentBO commentBO) {
        // 新增评论，主键commentId已保存在comment中
        LocalDateTime now = LocalDateTime.now();
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentBO, comment);
        comment.setCreatedTime(now);
        commentMapper.insert(comment);

        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(comment, commentVO);
        Users user = usersService.getById(commentBO.getCommentUserId());
        commentVO.setCommentId(comment.getId());
        commentVO.setCommentUserNickname(user.getNickname());
        commentVO.setCommentUserFace(user.getFace());
        commentVO.setCreatedTime(now);
        return commentVO;
    }

    /**
     * 查询朋友圈评论
     *
     * @param friendCircleId 朋友圈ID
     * @return
     */
    @Override
    public List<CommentVO> queryComment(String friendCircleId) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("friendCircleId", friendCircleId);
        return commentMapper.queryComment(paramsMap);
    }

}
